﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fcdbas.WinUI.Forms;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using Fcdbas.Platform.Interface;
using Fcdbas.WinUI.Common;
using Fcdbas.Core.Windows;
using Fcdbas.WinUI.Windows;
using Fcdbas.Platform.Properties;
using Fcdbas.Platform;
using Fcdbas.WinUI.Controls;
using Fcdbas.Core;
using Fcdbas.Core.Reflection;

namespace Fcdbas.WinUI.Forms
{
    public partial class frmValueList : cDialogWindow
    {
        private string ValueListType;
        private string Where;
        private string OrderBy;
        private bool Multiple;
        /// <summary>
        /// 实例化值类型窗口
        /// </summary>
        /// <param name="vlType">数据实体类型</param>
        /// <param name="where">条件子句</param>
        /// <param name="order">排序子句</param>
        /// <param name="title">当前值标题</param>
        public frmValueList(string vlType,string where,string order,string title,bool Multiple)
        {
            InitializeComponent();
            gcValueList.IsLoadLayout = true;
            this.Multiple = Multiple;
            this.ValueListType = vlType;
            Where = where;
            OrderBy = order;
            LoadValueList(true,this.Where,this.OrderBy);
            this.Text = this.Text + " - " + title;

        }
        private void LoadValueList(bool IsFirst,string where,string orderby)
        {
            if (string.IsNullOrEmpty(ValueListType))
            {
                this.ShowMessage("值类型不能为空", "", MessageBoxButtons.OK);
                return;
            }
            Type[] tps = new Type[5];
            for (int i = 0; i < 5; i++)
            {
                if (i < 3)
                    tps[i] = typeof(string);
                if (i >= 3)
                    tps[i] = typeof(Int32);
            }
            Type t = Fcdbas.Core.Reflection.TypeX.GetType(this.ValueListType,true);
            if(t== null)
            {
                this.ShowMessage("设置的类型不对，请检查！", "", MessageBoxButtons.OK);
                return;
            }
            if (IsFirst)
            {
                var piList = t.GetProperties().Where(p => p.PropertyType.IsPublic == true).ToList();
                foreach (System.Reflection.PropertyInfo pi in piList)
                {
                    BindColumnAttribute bcAttr = pi.GetCustomAttribute<BindColumnAttribute>(false);
                    DisplayNameAttribute dAttr = pi.GetCustomAttribute<DisplayNameAttribute>(false);
                    if (bcAttr == null)
                        continue;
                    if (bcAttr.IsVisible)
                    {
                        string caption = "";
                        if (dAttr != null)
                            caption = dAttr.DisplayName;
                        else
                            caption = bcAttr.Name;
                        GridColumn gc = new GridColumn();
                        gc.FieldName = bcAttr.Name;
                        gc.Caption = caption;
                        gc.Name = bcAttr.Name;
                        gc.Visible = true;
                        gvValueList.Columns.Add(gc);
                    }
                }
            }
            MethodInfoX method = MethodInfoX.Create(t, "FindAll", tps);
            IEntityList obj = method.Invoke(method, where, orderby, "", 0, 0) as IEntityList;
            
            this.MasterdataSourceBinding.DataSource = obj;
            
            this.gcValueList.RefreshDataSource();
             
            
        }

        private void SetSysColumnVisible()
        {
            Type t = Fcdbas.Core.Reflection.TypeX.GetType(this.ValueListType, true);
            if (t == null)
            {
                //this.ShowMessage("设置的类型不对，请检查！", "", MessageBoxButtons.OK);
                return;
            }
            var piList = t.GetProperties().Where(p => p.PropertyType.IsPublic == true).ToList();
            foreach (System.Reflection.PropertyInfo pi in piList)
            {
                BindColumnAttribute bcAttr = pi.GetCustomAttribute<BindColumnAttribute>(false);
                if (bcAttr == null)
                    continue;
                foreach (GridColumn gc in this.gvValueList.Columns)
                {
                    if (bcAttr.Name.EqualIgnoreCase(gc.FieldName) && !bcAttr.IsVisible)
                        gc.Visible = false;
                }
            }
            this.MasterdataSourceBinding.Columns.Clear();
            foreach (GridColumn gc in this.gvValueList.Columns)
            {
                switch (gc.FieldName.ToUpper())
                {
                    case "OBJID":
                        gc.Visible = false;
                        break;
                    case "OBJVERSION":
                        gc.Visible = false;
                        break;
                    case "OLDOBJVERSION":
                        gc.Visible = false;
                        break;
                    case "RESULT":
                        gc.Visible = false;
                        break;
                    case "STATUS":
                        gc.Visible = false;
                        break;
                    case "REFERENCE1":
                        gc.Visible = false;
                        break;
                    case "REFERENCE2":
                        gc.Visible = false;
                        break;
                    case "REFERENCE3":
                        gc.Visible = false;
                        break;
                    case "REFERENCE4":
                        gc.Visible = false;
                        break;
                    case "REFERENCE5":
                        gc.Visible = false;
                        break;
                    case "REFERENCE6":
                        gc.Visible = false;
                        break;
                    case "REFERENCE7":
                        gc.Visible = false;
                        break;
                    case "REFERENCE8":
                        gc.Visible = false;
                        break;
                    case "REFERENCE9":
                        gc.Visible = false;
                        break;
                    case "REFERENCE10":
                        gc.Visible = false;
                        break;
                        
                    default:
                        string caption = gc.GetTextCaption();
                        
                        DataItem di = new DataItem(gc.FieldName, gc.ColumnType, 100, "", false, false, false, true, "", "", "", caption, "", false, false, Platform.Enums.DataItemFlags.Attribute, Platform.Enums.DataItemUpdate.NotAllowed);
                        this.MasterdataSourceBinding.Columns.Add(di);
                        break;
                }
            }
        }
        /// <summary>
        /// 取得选择数据的值，以30，31为分隔符分隔
        /// </summary>
        public string SelectEntity
        {
            get
            {
                if (!Multiple)
                {
                    if (this.MasterdataSourceBinding.Current != null)
                    {
                        IEntity en = MasterdataSourceBinding.Current as IEntity;
                        if (en != null)
                            return en.GetAttr(false);
                        else
                            return string.Empty;
                    }
                }
                else
                {
                    int[] selects = gvValueList.GetSelectedRows();
                    string res = "";
                    foreach (int idx in selects)
                    {
                        IEntity et = gvValueList.GetRow(idx) as IEntity;
                        if (et != null)
                        {
                            res += et.GetAttr(false);
                            res += ClientSys.group_separator_;
                        }

                    }
                    return res;
                }
                return string.Empty;

            }
        }

        public string Key
        {
            get
            {
                Type t = Fcdbas.Core.Reflection.TypeX.GetType(this.ValueListType, true);
                if (t == null)
                {
                    //this.ShowMessage("设置的类型不对，请检查！", "", MessageBoxButtons.OK);
                    return "";
                }
                string columnName = "";
                var piList = t.GetProperties().Where(p => p.PropertyType.IsPublic == true).ToList();
                foreach (System.Reflection.PropertyInfo pi in piList)
                {
                    BindColumnAttribute bcAttr = pi.GetCustomAttribute<BindColumnAttribute>(false);
                    if (bcAttr == null)
                        continue;
                    if (bcAttr.Order == 1)
                    {
                        columnName = bcAttr.Name;
                        break;
                    }
                }
                if (!Multiple)
                {
                    if (this.MasterdataSourceBinding.Current != null)
                    {
                        IEntity en = MasterdataSourceBinding.Current as IEntity;
                        if (en != null)
                            return en[columnName].ToString();
                        else
                            return string.Empty;
                    }
                }
                else
                {
                    int[] selects = gvValueList.GetSelectedRows();
                    string res = "";
                    foreach (int idx in selects)
                    {
                        IEntity et = gvValueList.GetRow(idx) as IEntity;
                        if (et != null)
                        {
                            res += et[columnName].ToString();
                            res += ClientSys.group_separator_;
                        }

                    }
                    return res;
                }
                return string.Empty;
            }
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void frmValueList_Load(object sender, EventArgs e)
        {
            this.gvValueList.OptionsSelection.MultiSelect = true;
        }

        private void frmValueList_Activated(object sender, EventArgs e)
        {
            SetSysColumnVisible();
        }

        private void gvValueList_DoubleClick(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnList_Click(object sender, EventArgs e)
        {
            LoadValueList(false, GetWhere(), this.OrderBy);
        }
        private string UserWhere = "";
        private void btnQuary_Click(object sender, EventArgs e)
        {
            frmQuery queryForm = new frmQuery(this,this.UQProfile);
            if (queryForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                UserWhere = queryForm.Where;
               
                LoadValueList(false, GetWhere(), this.OrderBy);
            }
        }
        private string GetWhere()
        {
            string where = Where;
            if (!string.IsNullOrEmpty(where) && !string.IsNullOrEmpty(this.UserWhere))
                where = Where + " and " + this.UserWhere;
            else if (!string.IsNullOrEmpty(this.UserWhere))
                where = this.UserWhere;
            return where;
        }
    }
}
